package com.zdsoft.blp.zongbuju.shouye.linliquan.activity;

import android.content.Intent;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.zdsoft.blp.R;
import com.zdsoft.blp.gongyong.API.API;
import com.zdsoft.blp.gongyong.fanhui.FanHuiView;
import com.zdsoft.blp.gongyong.imageFrame.IvFrameView;
import com.zdsoft.blp.gongyong.imageTrans.CircleImage;
import com.zdsoft.blp.gongyong.net.BaseActivity;
import com.zdsoft.blp.gongyong.net.Net;
import com.zdsoft.blp.gongyong.net.NetCallback;
import com.zdsoft.blp.gongyong.xlistview.XListView;
import com.zdsoft.blp.zongbuju.shouye.linliquan.adapter.LLPingLunAdapter;
import com.zdsoft.blp.zongbuju.shouye.linliquan.entity.LinLi;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/9/5.
 */
public class XWXQXWActivity extends BaseActivity implements NetCallback{
        private IvFrameView contentImage;
        private ListView qz_xlv;
        private TextView ershou_name,ershou_date,ershou_title,lianxiren,ershou_content,ershou_dianhua,ershou_pinglun;
        private ImageView ershou_touxiang,pinglun;
        private Intent intent;
        private List<LinLi> list;
        private FanHuiView qz_title;
        private  String id;
        private int pageNo=0;
        private String huifuTime,touxiang,huifuUser,huifuImage,huifuContent;
        @Override
        protected void init() {
            setContentView(R.layout.linli_qiuzhixiangqing);
//        实例化控件
            qz_xlv = find(R.id.qz_xlv);
            qz_xlv.setFocusable(false);
            ershou_name = find(R.id.ershou_name);
            ershou_date = find(R.id.ershou_date);
            ershou_title = find(R.id.ershou_title);
            ershou_content = find(R.id.ershou_content);
            ershou_dianhua = find(R.id.ershou_dianhua);
            pinglun = find(R.id.pinglun);
            ershou_pinglun = find(R.id.ershou_pinglun);
            ershou_touxiang = find(R.id.ershou_touxiang);
            contentImage = find(R.id.contentImage);
            lianxiren = find(R.id.lianxiren);
            qz_title = find(R.id.qz_title);
            qz_xlv.setOnTouchListener(new View.OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    return true;
                }
            });
            //接收数据
            intent = getIntent();
            id=intent.getStringExtra("id");
            String title=intent.getStringExtra("title");
            qz_title.getTv_title().setText(title);
            getNet();//实例化Net的方法
            pinglun.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    intent=new Intent(XWXQXWActivity.this,LLHFActivity.class);
                    intent.putExtra("id","6");
                    startActivity(intent);
                }
            });
        }

    public void getNet() {
        Net net = new Net();
        net.url = API.INFOEXCHANGE_URL;//infoExchangeService
        net.nameSpace = API.INFOEXCHANGE_NAMESPACE;
        net.methodName = API.FOUNDLOSTCONTENT;//foundLostContent
        Map map = new HashMap();
        map.put("id", id);
        net.parms = map;
        getServer(net, this);
    }
//回调接口并解析JOSN 并赋值
    @Override
    public void onResponse(boolean flag, int code, String res) {
        Log.e("寻物招领",res);
        try {
            JSONObject object=new JSONObject(res);
            ershou_date.setText(object.getString("createTime"));
            Picasso.with(this).load(object.getString("headimgurl")).transform(new CircleImage()).into(ershou_touxiang);
            ershou_name.setText(object.getString("userName"));
            ershou_title.setText(object.getString("title"));
            ershou_content.setText(object.getString("content"));
            lianxiren.setText(object.getString("contact"));
            ershou_dianhua.setText(object.getString("contactPhone"));
            ershou_pinglun.setText(object.getString("replyNum"));
            boolean picPathSet=object.isNull("picPathSet");
            if (picPathSet==false){
                JSONArray array=object.getJSONArray("picPathSet");
                for (int i=0;i<array.length();i++){
                    JSONObject obj=array.getJSONObject(i);
                    String contentimage=obj.getString("picPath");
                    if (!"".equals(contentimage)){
                        contentImage.setVisibility(View.VISIBLE);
                        contentImage.show(new String[]{contentimage});
                    }else {
                        contentImage.setVisibility(View.GONE);
                    }
                }
            }
            //解析回复
            boolean replySet=object.isNull("replySet");
            if (replySet==false){
                list=new ArrayList<>();
                LinLi pinche=null;
                JSONArray pingL=object.getJSONArray("replySet");
                for (int i=0;i<pingL.length();i++){
                    JSONObject pc_huifu=pingL.getJSONObject(i);
                    huifuUser=pc_huifu.getString("replyUserName");
                    huifuTime=pc_huifu.getString("replyTime");
                    huifuContent=pc_huifu.getString("content");
                    huifuImage=pc_huifu.getString("replyUserHeadimgurl");
                    pinche=new LinLi(huifuUser,huifuContent,huifuImage,huifuTime);
                    list.add(pinche);
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        LLPingLunAdapter adapter = new LLPingLunAdapter(list, getApplicationContext());
        qz_xlv.setAdapter(adapter);
    }
}
